package main

import (
	"fmt"
	"strings"
)

var pow2 = []int{1, 2, 4, 8, 16, 32, 64, 128}

func main() {
	s := []int{2, 3, 5, 7, 11, 13}
	fmt.Println("s ==", s)

	for i := 0; i < len(s); i++ {
		fmt.Printf("s[%d] == %d\n", i, s[i])
	}

	// Create a tic-tac-toe board.
	game := [][]string{
		[]string{"_", "_", "_"},
		[]string{"_", "_", "_"},
		[]string{"_", "_", "_"},
	}

	// The players take turns.
	game[0][0] = "X"
	game[2][2] = "O"
	game[2][0] = "X"
	game[1][0] = "O"
	game[0][2] = "X"
	printBoard(game)

	ss := []int{2, 3, 5, 7, 11, 13}
	fmt.Println("ss ==", ss)
	fmt.Println("ss[1:4] ==", ss[1:4])

	// 省略下标代表从 0 开始
	fmt.Println("ss[:3] ==", ss[:3])

	// 省略上标代表到 len(ss) 结束
	fmt.Println("ss[4:] ==", ss[4:])

	a := make([]int, 5)
	printSlice("a", a)
	b := make([]int, 0, 5)
	printSlice("b", b)
	c := b[:2]
	printSlice("c", c)
	d := c[2:5]
	printSlice("d", d)

	var z []int
	fmt.Println(z, len(z), cap(z))
	if z == nil {
		fmt.Println("nil!")
	}

	var aa []int
	printSlice("aa", aa)

	// append works on nil slices.
	aa = append(aa, 0)
	printSlice("aa", aa)

	// the slice grows as needed.
	aa = append(a, 1)
	printSlice("aa", aa)

	// we can add more than one element at a time.
	aa = append(aa, 2, 3, 4)
	printSlice("aa", aa)

	for i, v := range pow2 {
		fmt.Printf("2**%d = %d\n", i, v)
	}

	pow2 := make([]int, 10)
	for i := range pow2 {
		pow2[i] = 1 << uint(i)
	}
	for _, value := range pow2 {
		fmt.Printf("%d\n", value)
	}
}

func printBoard(s [][]string) {
	for i := 0; i < len(s); i++ {
		fmt.Printf("%s\n", strings.Join(s[i], " "))
	}
}

func printSlice(s string, x []int) {
	fmt.Printf("%s len=%d cap=%d %v\n",
		s, len(x), cap(x), x)
}
